Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add TrustManagerFactory workaround for Conscrypt bug #1993

Merged
merged 1 commit into from
May 24, 2024
Merged

Conversation

hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented May 21, 2024

This is a workaround for an underlying bug in the Google Conscrypt crypto library
google/conscrypt #1033.
The root cause is that the Conscrypt and OpenJDK X509 certificate libraries sometimes interpret the AuthType
field differently: Conscrypt finds 'GENERIC' auth type when OpenJDK finds 'UNKNOWN' auth type. This causes certificate validation to fail.

The workaround implemented here is to add a delegate TrustManager that replaces 'GENERIC' auth type with 'UNKNOWN' auth type so that the Conscrypt crypto plays nice with the JDK crypto. See comment on #1033.

I manually tested this on a modified JVM that used Conscrypt as it's primary crypto library. The integration tests passed. I have not found a good way to make this test part of the test suite.

Fixes #1983

Copy link
Member

@enocom enocom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fixing the issue or forcing all customers to use Conscrypt?

Could we make this a feature by allowing customers to opt-in to conscript with a configuration property?

@hessjcg
Copy link
Collaborator Author

hessjcg commented May 22, 2024

This is adding an adapter so that if a customer's JVM has Conscrypt, then the connector will work around this Conscrypt bug and continue to work correctly. I will add some logic so that the connector only uses the workaround if Conscript is enabled.

@hessjcg hessjcg requested a review from enocom May 22, 2024 16:29
@enocom enocom changed the title fix: Add TrustManagerFactory workaround for Conscrypt bug, Fixes #1983. fix: Add TrustManagerFactory workaround for Conscrypt bug May 22, 2024
Provider prov = ctx.getProvider();
log.info("TLS Provider: {}", prov.getName());
} catch (NoSuchAlgorithmException e) {
// handle exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should we be doing here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rethrow exception.

@@ -0,0 +1,13 @@
security.provider.1=Conscrypt
security.provider.2=SUN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be setting all of these here or just a few?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should all be here, but Conscrypt should come first so it gets loaded first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this big file? Is everything really necessary here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file fully replaces the built-in JVM security properties. So we need to set all of the default security properties, not just the few properties that are relevant to prioritizing the Conscrypt JCE library.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we say that in a comment in the file? Future maintainers are going to struggle to understand why this is here otherwise. Also, do we need all the extra whitespace below?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some comments to the file to clarify.

pom.xml Show resolved Hide resolved
@hessjcg hessjcg force-pushed the gh-1983-fix branch 5 times, most recently from 6b4dfde to 31a72fa Compare May 22, 2024 20:46
@@ -229,5 +229,23 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>google-conscript</id>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this profile for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This profile allows you to run the Postgres JDBC integration tests using Conscrypt crypto.

@@ -0,0 +1,13 @@
security.provider.1=Conscrypt
security.provider.2=SUN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this big file? Is everything really necessary here?

pom.xml Show resolved Hide resolved
// Note: This is a workaround for Conscrypt bug #1033
// Conscrypt is the JCE provider on some Google Cloud runtimes like DataProc.
// https://github.com/google/conscrypt/issues/1033
if (ConscryptWorkaroundTrustManagerFactory.isWorkaroundNeeded()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do these lines relate to ConscryptWorkaroundTrustManagerFactorySpi? Do we need both?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the JVM, we need to implement 3 classes to make sure that we are capturing all of the TrustManager instances created by the default Java Crypto provider and wrapping them with the ConscryptWorkaroundTrustManager:

  • class ConscryptWorkaroundTrustManagerFactory extends TrustManagerFactory - has a bunch of final methods that delegate to a TrustManagerFactorySpi.
  • class ConscryptWorkaroundTrustManagerFactorySpi implements TrustManagerFactorySpi - can actually intercept and delegate calls related to trust managers and wrap them with ConscryptWorkaroundTrustManager
  • ConscryptWorkaroundTrustManager - the workaround for the Conscrypt bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks. Personally, this context would be useful to me in the future so I'd love to see this capture in the commit message or in a comment somewhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this extra info to the ConscryptWorkaroundTrustManagerFactory class javadoc.

@hessjcg hessjcg requested a review from enocom May 23, 2024 17:45
Copy link
Member

@enocom enocom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should frame this as a "feat" since there's no bug here in our code.

// Note: This is a workaround for Conscrypt bug #1033
// Conscrypt is the JCE provider on some Google Cloud runtimes like DataProc.
// https://github.com/google/conscrypt/issues/1033
if (ConscryptWorkaroundTrustManagerFactory.isWorkaroundNeeded()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks. Personally, this context would be useful to me in the future so I'd love to see this capture in the commit message or in a comment somewhere.

@@ -0,0 +1,13 @@
security.provider.1=Conscrypt
security.provider.2=SUN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we say that in a comment in the file? Future maintainers are going to struggle to understand why this is here otherwise. Also, do we need all the extra whitespace below?

@hessjcg
Copy link
Collaborator Author

hessjcg commented May 24, 2024

I added comments for clarity.

@hessjcg hessjcg merged commit 0735a91 into main May 24, 2024
17 checks passed
@hessjcg hessjcg deleted the gh-1983-fix branch May 24, 2024 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Getting Unknown authType: GENERIC when connecting to postgres
3 participants